package com.tedu.bill.demo4;

/**
 * Author：毕龙
 * Date: 2024/5/24 15:14
 */
public class Train2 extends Thread{
    private static int ticket;//火车票

    @Override
    public void run() {
        while (true){
            //用类的信息作为锁标记 因为类的信息是唯一的
           synchronized (Train2.class){
               //当票卖了120张时 退出循环
               if(ticket == 120){
                   break;
               }
               try {
                   Thread.sleep(100);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               ticket ++;

               System.out.println(Thread.currentThread().getName()+"第"+ticket+"张票卖出");
           }
        }
    }
}
